Send and receive file using System.Net.Sockets.Socket in C#

您所在的位置:网站首页 socket file Send and receive file using System.Net.Sockets.Socket in C#

Send and receive file using System.Net.Sockets.Socket in C#

2023-04-08 13:37| 来源: 网络整理| 查看: 265

You can use any TCP receive method to get the data.  YOu also need to develope an higher level application protocol for sending data. th equestion you need to ask is how does the the client know that this data being sent is a file and that the connection is going to close after the file is sent.  You have a few problems with your application level protocol.

Thre must be a master/slave relationship between the client and server.  Te client is the master that sends commands to the server and the server is a slave which processes the conmands.  For the server to send the file the client need to send a command requesting the file. The server can't jsut send a file without the client expecting a file.

You also have a problem with the client closing the connection at the end of the file.  the conection close should only be done from one end of the connection after all the data is recevied.  A binary should be sent with a byte count added a the beginning of the file.  A text file cxan be sent eith er by adding a byte count to the beginning of the files transfer or ending the text data with a '\0'.  This is how I would impliment a download of a text file.

Client                                                                Server

Send command : download file abc  ---->         Process command

Read data until '\0' character                   Process command and close connection

Wait for Asyn close event.  Exit Application        Exit Application

jdweng



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3